#include "tank.h"
#include "ui_tank.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
#include<QtGlobal>

tank::tank(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::tank),start(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(200,200,520,520));
}

tank::~tank()
{
    delete ui;
}
int map[26][26] = {  { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1 },
                     { 2,2,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,0,0,0,0,0,0,0,0 } };


void tank::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(!start)Inittank();
    for(int i=0;i<26;i++)
    {
        for(int j=0;j<26;j++)
        {
            if(map[i][j]==0)//空地
            {
                 //painter.setPen(Qt::black);
                 painter.setBrush(Qt::black);
                 painter.drawRect(j*20,i*20,20,20);
            }
            if(map[i][j]==1)//土块
            {
                //painter.setPen(Qt::green);
                //painter.setBrush(Qt::red);
                //painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/wall1.jpg"));
            }
            if(map[i][j]==2)//铁块
            {
                //painter.setPen(Qt::green);
                //painter.setBrush(Qt::green);
                //painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/wall2.jpg"));
            }
            if(map[i][j]==4)//基地
            {
                //painter.setBrush(Qt::black);
                //painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/home.jpg"));
            }
            /*if(map[i][j]==8)//enemy
            {
                int t=0;
                 //painter.setPen(Qt::black);
                 painter.setBrush(Qt::green);
                 painter.drawRect(j*20,i*20,20,20);
                 for(t=0;t<2;t++)
                 {
                     if(enemy[t].x==i)t=i;
                 }
                 if(enemy[t].direction==1)painter.drawPixmap(enemy[t].y*20,t*20,20,20,QPixmap(":/images/images/up.PNG"));
                 if(enemy[t].direction==2)painter.drawPixmap(enemy[t].y*20,t*20,20,20,QPixmap(":/images/images/down.PNG"));
                 if(enemy[t].direction==3)painter.drawPixmap(enemy[t].y*20,t*20,20,20,QPixmap(":/images/images/left.PNG"));
                 if(enemy[t].direction==4)painter.drawPixmap(enemy[t].y*20,t*20,20,20,QPixmap(":/images/images/right.PNG"));

            }*/
            if(map[i][j]==9)//子弹
            {
                //painter.setBrush(Qt::blue);
               // painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/bullet2.PNG"));
            }
            if(map[i][j]==5)//mytank
            {
                //painter.setBrush(Qt::yellow);
                //painter.drawRect(j*20,i*20,20,20);
                if(ndirection==1)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_up.jpg"));
                if(ndirection==2)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_down.jpg"));
                if(ndirection==3)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_left.jpg"));
                if(ndirection==4)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_right.jpg"));
            }
        }
    }
    for(int t=0;t<2;t++)
    {
        if(enemy[t].direction==1)painter.drawPixmap(enemy[t].y*20,enemy[t].x*20,20,20,QPixmap(":/images/images/up.PNG"));
        if(enemy[t].direction==2)painter.drawPixmap(enemy[t].y*20,enemy[t].x*20,20,20,QPixmap(":/images/images/down.PNG"));
        if(enemy[t].direction==3)painter.drawPixmap(enemy[t].y*20,enemy[t].x*20,20,20,QPixmap(":/images/images/right.PNG"));
        if(enemy[t].direction==4)painter.drawPixmap(enemy[t].y*20,enemy[t].x*20,20,20,QPixmap(":/images/images/left.PNG"));

    }
}
void tank::Inittank()
{
   start=true;
   mytank.x=24;mytank.y=9;
   map[mytank.x][mytank.y]=5;
   ndirection=1;
   create_enemy();

   mytanktimer=new QTimer(this);
   mytanktimer->start(399);
   connect(mytanktimer,SIGNAL(timeout()),SLOT(tank_update()));

   mybullettimer=new QTimer(this);
   mybullettimer->start(100);
   connect(mybullettimer,SIGNAL(timeout()),SLOT(bullet_update()));

   enemytimer=new QTimer(this);
   enemytimer->start(2000);
   connect(enemytimer,SIGNAL(timeout()),SLOT(enemy_update()));

   attacktimer=new QTimer(this);
   attacktimer->start(100);
   connect(attacktimer,SIGNAL(timeout()),SLOT(attack()));

   updatetimer=new QTimer(this);
   updatetimer->start(51);
   connect(updatetimer,SIGNAL(timeout()),SLOT(update()));
}
void tank::attack()
{
    for(int i=0;i<2;i++)
    {
        if(map[enemy[i].x][enemy[i].y]==9)
        {
            enemy[i].life=false;
            create_enemy();
        }
    }
}

void tank::enemy_update()
{
    for(int i=0;i<2;i++)
    {
        if(enemy[i].life==true)
        {
            map[enemy[i].x][enemy[i].y]=0;
            int x=enemy[i].x,y=enemy[i].y;
            switch(enemy[i].direction)
             {
                 case 1:
                {
                    if(map[x-1][y]!=1&&map[x-1][y]!=2&&x>0)enemy[i].x--;
                     break;
                }
                 case 2:
                {
                    if(map[x+1][y]!=1&&map[x+1][y]!=2&&x<25)enemy[i].x++;
                     break;
                }
                 case 3:
                {
                    if(map[x][y-1]!=1&&map[x][y-1]!=2&&y>0)enemy[i].y--;
                     break;
                }
                 case 4:
                {
                    if(map[x][y+1]!=1&&map[x][y+1]!=2&&y<25)enemy[i].y++;
                     break;
                }
                 default:;
             }
            enemy[i].direction=2+rand()%3;
            map[enemy[i].x][enemy[i].y]=8;
            }
    }
    for(int i=0;i<2;i++)
    {
        if(enemy[i].life==true)
        {
            for(int j=0;j<100;j++)
            {
                if(bullet[j].life==false)
                {
                    bullet[j].direction=enemy[i].direction;
                    bullet[j].x=enemy[i].x; bullet[j].y=enemy[i].y;
                    bullet[j].life=true;
                    break;
                }
            }
        }
    }
}

void tank::bullet_update()
{
    if(shoot==true)
    {
        for(int i=0;i<100;i++)
        {
            if(bullet[i].life==false)
            {
                bullet[i].direction=ndirection;
                if(ndirection==1){bullet[i].x=mytank.x--; bullet[i].y=mytank.y;}
                if(ndirection==2){bullet[i].x=mytank.x++; bullet[i].y=mytank.y;}
                if(ndirection==3){bullet[i].x=mytank.x; bullet[i].y=mytank.y--;}
                if(ndirection==4){bullet[i].x=mytank.x; bullet[i].y=mytank.y++;}
                //bullet[i].x=mytank.x; bullet[i].y=mytank.y;
                bullet[i].life=true;
                break;
            }
        }
        shoot=false;
    }
    for(int i=0;i<100;i++)
    {
        if(bullet[i].life==true)
        {
            map[bullet[i].x][bullet[i].y]=0;
            if(bullet[i].direction==1)bullet[i].x--;
            if(bullet[i].direction==2)bullet[i].x++;
            if(bullet[i].direction==3)bullet[i].y--;
            if(bullet[i].direction==4)bullet[i].y++;
            if(map[bullet[i].x][bullet[i].y]==4)
            {
                mytanktimer->stop();
                mybullettimer->stop();
            }
            if(map[bullet[i].x][bullet[i].y]==8)
            {
                bullet[i].life=false;
                for(int t=0;;t++)
                {
                    if(enemy[t].x==bullet[i].x){enemy[i].life=false;break;}
                }
            }
            if(map[bullet[i].x][bullet[i].y]==2)
            {
                bullet[i].life=false;
                break;
            }
            if(bullet[i].x<=0||bullet[i].x>=26||bullet[i].y<0||bullet[i].y>=26||map[bullet[i].x][bullet[i].y]==1)
            {
                bullet[i].life=false;
                map[bullet[i].x][bullet[i].y]=0;
            }
            else map[bullet[i].x][bullet[i].y]=9;
        }
    }
   //update();
}

void tank::tank_update()
{
    map[mytank.x][mytank.y]=0;
    int x=mytank.x,y=mytank.y;
    switch(ndirection)
     {
     case 1:
    {
        if(map[x-1][y]!=1&&map[x-1][y]!=2&&x>0)mytank.x--;
         break;
    }
     case 2:
    {
        if(map[x+1][y]!=1&&map[x+1][y]!=2&&x<26)mytank.x++;
         break;
    }
     case 3:
    {
        if(map[x][y-1]!=1&&map[x][y-1]!=2&&y>0)mytank.y--;
         break;
    }
     case 4:
    {
        if(map[x][y+1]!=1&&map[x][y+1]!=2&&y<26)mytank.y++;
         break;
    }
     default:;
     }
    map[mytank.x][mytank.y]=5;

}


void tank::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key())
    {
    case Qt::Key_W:ndirection=1;
       break;
    case Qt::Key_S:ndirection=2;
     break;
    case Qt::Key_A:ndirection=3;
     break;
    case Qt::Key_D:ndirection=4;
     break;
    case Qt::Key_J:shoot=true;
       break;
     default:;
    }
}

void tank::create_enemy()
{
    for(int i=0;i<2;i++)
    {
        if(enemy[i].life==false)
         {
            enemy[i].life=true;
            while(1)
            {
                int x,y;
                x=rand()%26;
                y=rand()%26;
                if(map[x][y]==0)
                {
                    map[x][y]=8;
                    enemy[i].x=x;enemy[i].y=y;
                    enemy[i].direction=1+rand()%3;
                    break;
                }
            }
         }
        }
}
